﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMLAB : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();

        public FMLAB()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='A_TS_POINT'";
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select * from a_ts_point "
              + " where fpoint_flag = 2";
            if (qifpoint_no.Text !="")
            {
                SQL = SQL + " and ((fpoint_no like '%"+ qifpoint_no.Text+"%')"
                 +" or (fpoint_name like '%"+ qifpoint_no.Text+"%'))";
            }
            SQL = SQL + "  order by fpoint_no";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }

            rowindex = -1;
        }

        private void FMLAB_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_lab";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Visible = false; sys11.Tag = "1";

            //执行查询代码
            OnSys04();
        }

        //新增代码
        protected override void OnSys01()
        {
            uifpoint_no.Text = "";
            uifpoint_name.Text = "";
            
            sType = "A";
            doLoadData();
            uifpoint_no.ReadOnly = false;
            uifpoint_name.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            if (!sType.Equals("A"))
            {
                uifpoint_no.Text = uiDGView["fpoint_no", rowindex].Value.ToString();
                uifpoint_name.Text = uiDGView["fpoint_name", rowindex].Value.ToString();
            }
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifpoint_no.ReadOnly = true;
            uifpoint_name.ReadOnly = false;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fpoint_name", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fpoint_no", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'a_ts_lab','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();
            uifpoint_no.ReadOnly = true;
            uifpoint_name.ReadOnly = true;

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifpoint_no, "请输入实验楼编号")
                   && CheckEmpty(uifpoint_name, "请输入实验楼名称");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        a_ts_point myData = context.a_ts_point.Find(uifpoint_no.Text);
                        if (myData is null)
                        {
                            a_ts_point myData2 = new a_ts_point();
                            myData2.fpoint_no = uifpoint_no.Text;
                            myData2.fpoint_name = uifpoint_name.Text;
                            myData2.fparent_no = "0";
                            myData2.fpoint_flag = 2;
                            myData2.fpoint_level = 1;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            myData2.fpoint_state = 1;
                            context.a_ts_point.Add(myData2);
                            context.SaveChanges();
                            doProc();
                            UIMessageTip.ShowOk("添加成功");

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    a_ts_point myts_point = context.a_ts_point.Find(uifpoint_no.Text);

                    myts_point.fpoint_name = uifpoint_name.Text;
                    myts_point.foperator = sUser;
                    myts_point.fupddate = DateTime.Now;
                    int result = context.SaveChanges();
                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

    }
}
